package com.sgb.kjwl.viewmodel.self.mine;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.self.CompanyFocusEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.fragment.self.mine.IntelligentFocusListFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class IntelligentFocusFragmentModel extends BaseViewModel {
    private IntelligentFocusListFragment intelligentAttentionListFragment;
    private SelfRepertory repertory;
    private int page = 0;

    public IntelligentFocusFragmentModel(IntelligentFocusListFragment intelligentAttentionListFragment, SelfRepertory repertory) {
        this.repertory = repertory;
        this.intelligentAttentionListFragment = intelligentAttentionListFragment;
    }

    //获取企业关注列表
    public void getDataList(boolean isLoadMore,String userNo) {
            page = isLoadMore ? page + 1 : 1;
            DialogHelper.showProgressDialog(intelligentAttentionListFragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
            APIManager.getInstance().getUserAPI().getCompanyFocus(page,20,userNo).enqueue(new Callback<BaseEntity<ListResponse<List<CompanyFocusEntity>>>>() {
                @Override
                public void onResponse(Call<BaseEntity<ListResponse<List<CompanyFocusEntity>>>> call, Response<BaseEntity<ListResponse<List<CompanyFocusEntity>>>> response) {
                    DialogHelper.dismissProgressDialog();
                    if (response.body() == null || response.body().getState() == null) {
                        MToast.showLongToast(intelligentAttentionListFragment.getActivity(), "服务连接超时请检查网络");
                        return;
                    }
                    if (response.body().getState().equals(APIManager.RESPONSE_OK) && response.body().getData() != null) {
                        intelligentAttentionListFragment.showList(response.body().getData().getList(),isLoadMore);
                    } else {
                        MToast.showLongToast(intelligentAttentionListFragment.getActivity(), response.body().getMsg());
                    }
                }

                @Override
                public void onFailure(Call<BaseEntity<ListResponse<List<CompanyFocusEntity>>>> call, Throwable t) {
                    DialogHelper.dismissProgressDialog();
                }
            });
        }
    //取消企业关注
    public void focus(int type,String focusNo,String userNo) {
            DialogHelper.showProgressDialog(intelligentAttentionListFragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
            APIManager.getInstance().getUserAPI().focus(type,focusNo,userNo).enqueue(new Callback<BaseEntity<Object>>() {
                @Override
                public void onResponse(Call<BaseEntity<Object>> call, Response<BaseEntity<Object>> response) {
                    DialogHelper.dismissProgressDialog();
                    if (response.body() == null || response.body().getState() == null) {
                        return;
                    }
                    if (response.body().getState().equals(APIManager.RESPONSE_OK) && response.body().getData() != null) {
                        intelligentAttentionListFragment.setAdapterStatus();
                    } else {
                        MToast.showLongToast(intelligentAttentionListFragment.getActivity(), response.body().getMsg());
                    }
                }

                @Override
                public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {
                    DialogHelper.dismissProgressDialog();
                }
            });
        }
}
